package com.ds.dsm.view.config.menu.service;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.menu.GalleryMenuPopTree;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.gallery.CustomGalleryViewBean;
import com.ds.esd.custom.gallery.enums.GalleryRowMenu;
import com.ds.esd.custom.grid.enums.GridMenu;
import com.ds.esd.custom.grid.enums.GridRowMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(path = "/dsm/view/config/gallery/")

public class GalleryMenuService {


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("GalleryMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GalleryMenuPopTree>> getGalleryMenuTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<GalleryMenuPopTree>> model = new TreeListResultModel<>();
        List<GalleryMenuPopTree> popTrees = new ArrayList<>();
        try {
            GalleryMenuPopTree menuPopTree = new GalleryMenuPopTree("gridMenu", "列表按钮");
            for (GridMenu menu : GridMenu.values()) {
                GalleryMenuPopTree item = new GalleryMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("methodName", methodName);
                item.addTagVar("viewInstId", viewInstId);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "列表按钮")
    @RequestMapping("RowMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<GalleryMenuPopTree>> getRowMenuTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<GalleryMenuPopTree>> model = new TreeListResultModel<>();
        List<GalleryMenuPopTree> popTrees = new ArrayList<>();
        try {
            GalleryMenuPopTree menuPopTree = new GalleryMenuPopTree("rowmenu", "列表按钮");
            for (GridRowMenu menu : GridRowMenu.values()) {
                GalleryMenuPopTree item = new GalleryMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("methodName", methodName);
                item.addTagVar("viewInstId", viewInstId);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @RequestMapping("addMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addMenu(String sourceClassName, String methodName, String GalleryMenuTree, String RowMenuTree, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            CustomGalleryViewBean galleryViewBean = (CustomGalleryViewBean) tableConfig.getSourceConfig().getMethodByName(methodName).getView();

            if (GalleryMenuTree != null && !GalleryMenuTree.equals("")) {
                String[] menuIds = StringUtility.split(GalleryMenuTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        galleryViewBean.getCustomMenu().add(GridMenu.valueOf(menuId));
                    }
                }
            } else if (RowMenuTree != null && !RowMenuTree.equals("")) {
                String[] menuIds = StringUtility.split(RowMenuTree, ";");
                for (String menuId : menuIds) {
                    if (menuId != null && !menuIds.equals("")) {
                        galleryViewBean.getRowCmdBean().getRowMenu().add(GalleryRowMenu.valueOf(menuId));
                    }
                }
            }

            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (
                JDSException e)

        {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }


    @RequestMapping("delMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delMenu(String sourceClassName, String methodName, String type, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            CustomGalleryViewBean galleryViewBean = (CustomGalleryViewBean) tableConfig.getSourceConfig().getMethodByName(methodName).getView();

            String[] menuIds = StringUtility.split(type, ";");
            for (String menuId : menuIds) {
                GridMenu gridMenu = GridMenu.fromType(menuId);
                if (gridMenu != null) {
                    galleryViewBean.getCustomMenu().remove(GridMenu.valueOf(menuId));
                }
                GridRowMenu rowMenu = GridRowMenu.fromType(menuId);
                if (rowMenu != null) {
                    galleryViewBean.getRowCmdBean().getRowMenu().remove(GridRowMenu.valueOf(menuId));

                }

            }
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }
        return model;
    }


}
